package com.woniu.custom.controller;


import com.woniu.common.utils.GetManageUserNickName;
import com.woniu.common.utils.PageParam;
import com.woniu.common.utils.ResponseData;
import com.woniu.custom.dto.CustomDto;
import com.woniu.custom.dto.CustomLevelDto;
import com.woniu.custom.dto.KefuDto;
import com.woniu.custom.pojo.ZyCustom;
import com.woniu.custom.pojo.ZyCustomLevel;
import com.woniu.custom.pojo.ZyUser;
import com.woniu.custom.service.ZyCustomLevelService;
import com.woniu.custom.service.ZyCustomService;
import com.woniu.custom.service.ZyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/custom")
public class CustomController {
    @Autowired
    private ZyCustomService customService;
    @Autowired
    private ZyCustomLevelService customLevelService;
    @Autowired
    private ZyUserService userService;
    @PostMapping("/delCustomLevel")
    public ResponseData<?> delCustomLevel(@RequestBody String id){
        String id2 = id.substring(0, id.length() - 1);
        ZyCustomLevel customLevel = customLevelService.getById(id2);
        customLevel.setCustomLevelStates(2);
        boolean b = customLevelService.saveOrUpdate(customLevel);
        if (b) {
            return new ResponseData<>().success();
        }else {
            return  new ResponseData<>().fail();
        }
    }

    @PostMapping("/addOrUpdateCustomLevel")
    public ResponseData<?> addOrUpdateCustomLevel(@RequestBody ZyCustomLevel customLevel){
        customLevel.setCustomLevelCreateTime(new Date());
        boolean b = customLevelService.saveOrUpdate(customLevel);
        if (b) {
            return new ResponseData<>().success();
        }else {
            return new ResponseData<>().fail();
        }
    }


    @PostMapping("/queryCustomLevelPage")
    public ResponseData<?> queryCustomLevelPage(@RequestBody PageParam<CustomLevelDto> pageParam){
        return customLevelService.queryCustomLevelPage(pageParam);
    }

    @PostMapping("/addCustom")
    public ResponseData<?> addCustom(@RequestBody ZyCustom custom) throws Exception {
        String userName = GetManageUserNickName.getUserName();
        custom.setCustomCreateBy(userName);
        custom.setCustomCreateTime(new Date());
        boolean save = customService.save(custom);
        if (save) {
            return new ResponseData<>().success();
        }else {
            return new ResponseData<>().fail();
        }
    }

    @PostMapping("/queryAllCustom")
    public ResponseData<?> queryAllCustom(@RequestBody PageParam<CustomDto> pageParam) {
       return customService.queryCustomPage(pageParam);
    }


    @PostMapping("/queryCustomLevel")
    public ResponseData<?> queryCustomLevel(){
        List<ZyCustomLevel> list = customLevelService.list();
        return new ResponseData<>().success(list);
    }


    @PostMapping("/queryAllUser")
    public ResponseData<?> queryAllUser(){
        List<ZyUser> list = userService.list();
        return new ResponseData<>().success(list);
    }


    @PostMapping("/laHei")
    public ResponseData<?> laHei(@RequestBody String[] ids){
        return customService.laHei(ids);
    }

    @PostMapping("/queryCustomDetail")
    public ResponseData<?> queryCustomDetail(@RequestBody String id){
        String s = id.substring(0, id.length() - 1);
        return customService.queryCustomDetail(s);
    }

    @GetMapping("/queryCustomByUserId")
    public ResponseData<?> queryCustomByUserId(@RequestParam("userId") String userId){
        ZyCustom one = customService.lambdaQuery().eq(ZyCustom::getUserId, userId).one();
        return new ResponseData<>().success(one);
    }

    @PostMapping("/saveAll")
    public ResponseData<?> saveAll(@RequestBody KefuDto kefuDto) throws InterruptedException {
        return customService.saveAll(kefuDto);
    }

}
